package com.example.spring.demo.service.impl;

import com.example.spring.demo.common.error.CustomException;
import com.example.spring.demo.entity.FileRecord;
import com.example.spring.demo.mapper.FileMapper;
import com.example.spring.demo.service.FileService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author qwert
 * @version 1.0.0
 * @create 2024/12/31 0:22
 **/
@Service
@RequiredArgsConstructor
public class FileServiceImpl implements FileService {
    private final FileMapper fileMapper;
    @Override
    public void saveFileRecord(FileRecord fileRecord) {
        int inserted = fileMapper.insert(fileRecord);
        if(inserted == 0) {
            throw new CustomException("文件保存数据库失败！");
        }
    }

    @Override
    public String getFileUrlByName(String fileName) {
        FileRecord fileRecord = fileMapper.getFileUrlByName(fileName);
        return fileRecord.getUrl();
    }

    @Override
    public List<String> getFileUrlListByCategory(String category) {
        List<FileRecord> fileRecordList = fileMapper.getFileUrlListByCategory(category);
        return fileRecordList.stream().map(FileRecord::getUrl).toList();
    }
}
